package com.fanqwz.kotlinmvvm.ui.fragment.main

import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelProviders
import com.fanqwz.kotlinmvvm.di.module.BaseFragmentModule
import com.fanqwz.kotlinmvvm.di.scope.PerFragment
import com.fanqwz.kotlinmvvm.domain.`object`.User
import com.fanqwz.kotlinmvvm.presenter.viewmodel.login.ILoginViewModel
import com.fanqwz.kotlinmvvm.presenter.viewmodel.login.LoginViewModel
import com.fanqwz.kotlinmvvm.ui.activity.login.ILoginView
import dagger.Binds
import dagger.Module
import dagger.Provides

/**
 * Created by fanqwz on 2017/10/31.
 */
@Module
abstract class MainFragmentModule(mainFragment: MainFragment) : BaseFragmentModule<MainFragment>() {

    @Binds
    @PerFragment
    abstract fun provideView(mainFragmentView: MainFragmentView): ILoginView

//    @Binds
//    @PerFragment
//    abstract fun provideViewModel(loginViewModel: LoginViewModel): ILoginViewModel

    @Module
    companion object {
        @Provides
        @JvmStatic
        @PerFragment
        fun user(): User = User(0, "test", "test")  // for test

//        @Provides
//        @PerFragment
//        @JvmStatic
//        fun providerFactory(loginViewModel: LoginViewModel): ViewModelProvider.Factory {
//            return object : ViewModelProvider.Factory {
//                override fun <T : ViewModel?> create(modelClass: Class<T>): T {
//                    if (modelClass.isAssignableFrom(LoginViewModel::class.java))
//                        return loginViewModel as T
//                    else
//                        throw IllegalArgumentException("class can't be inject:$modelClass")
//                }
//
//            }
//        }

        @Provides
        @PerFragment
        @JvmStatic
        fun provideViewModel(mainFragment: MainFragment, factory: ViewModelProvider.Factory): ILoginViewModel {
            return ViewModelProviders.of(mainFragment, factory).get(LoginViewModel::class.java)
        }

    }

}